package com.skymxc.wandroidtodo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;

import androidx.annotation.Nullable;

/**
 * <p>
 * 可以又拖动的 容器
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/12/4  10:32 下午
 */
public class SwipeLinearLayout extends LinearLayout {

    private Scroller scroller;

    public SwipeLinearLayout(Context context) {
        super(context);
    }

    public SwipeLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SwipeLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public SwipeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        scroller = new Scroller(context);
    }

    private int contentWidth;
    private int menuWidth;

    private View contentView;
    private View menuView;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentView = getChildAt(0);
        menuView = getChildAt(1);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        contentWidth = contentView.getMeasuredWidth();
        menuWidth = menuView.getMeasuredWidth();
    }

    private float startX;
    private int x;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startX = event.getX();
                break;
            case MotionEvent.ACTION_UP:
                float upEnd = event.getX();
                float thisUp = upEnd;
                // float myx = getScrollX() - thisUp;
                if (getScrollX() > menuWidth / 2) {
                    // x = myMenuWidth;

                    openM();
                } else {
                    // x = 0;

                    closeM();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int endX = (int) event.getX();
                int thisX = (int) (endX - startX);

                int scrollX = getScrollX();

                x = scrollX - thisX;

                // 非法值的屏蔽
                if (x < 0) {
                    x = 0;
                } else if (x > menuWidth) {
                    x = menuWidth;
                }

                scrollTo(x, getScrollY());

                startX = event.getX();

                break;

        }
        return true;
    }

    private void openM() {
        invalidate();
        int dx = menuWidth - getScrollX();
        scroller.startScroll(getScrollX(), getScrollY(), dx, Math.abs(1000));
    }

    private void closeM() {
        invalidate();
        int dx = 0 - getScrollX();
        scroller.startScroll(getScrollX(), getScrollY(), dx, Math.abs(1000));
    }

    @Override
    public void computeScroll() {
        super.computeScroll();

        if (scroller.computeScrollOffset()) {
            int currX = scroller.getCurrX();
            scrollTo(currX, 0);
            invalidate();
        }
    }
}
